package com.visionet.waveletData.web.service.account;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.visionet.waveletData.web.common.constant.SysConstants;
import com.visionet.waveletData.web.entity.account.Resource;
import com.visionet.waveletData.web.entity.account.Role;
import com.visionet.waveletData.web.entity.account.User;
import com.visionet.waveletData.web.entity.business.UserInfo;
import com.visionet.waveletData.web.interceptor.UserLogInterceptor;
import com.visionet.waveletData.web.repository.account.ResourceDao;
import com.visionet.waveletData.web.repository.account.UserDao;
import com.visionet.waveletData.web.repository.business.UserInfoDao;
import com.visionet.waveletData.web.service.BaseService;
import com.visionet.waveletData.web.vo.account.ResourceVo;
import com.visionet.waveletData.web.web.BaseController;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.mapper.BeanMapper;

import java.util.*;

/**
 * 资源 Service
 * 
 * @author xt
 */
//@Service
////默认将类中的所有public函数纳入事务管理.
//@Transactional(readOnly = true)
//public class ResourceService extends BaseService{
//	
//	private static Logger logger = LoggerFactory.getLogger(ResourceService.class);
//
//	@Autowired
//	private ResourceDao resourceDao;
//	@Autowired
//	private UserDao userDao;
//    @Autowired
//    private UserInfoDao userInfoDao;
//
//
//    @Transactional(readOnly = false)
//    public void save(Resource entity) {
//        resourceDao.save(entity);
//    }
//	
////	public List<Resource> findLeftMenuByUser(Long userId) {
////		if(BaseController.hasRole(SysConstants.ADMIN)){
////			return resourceDao.findLeftMenuByUser();
////		}else{
////			User user = userDao.findOne(userId);
////			List<String> permissions  = getPermissionList(user);
////			if(permissions.isEmpty()) return null;
////			
////			return resourceDao.findLeftMenuByUser(permissions);
////		}
////		
////	}
//	
//	public List<ResourceVo> findHeaderMenuByUser() {
//		List<Resource> resourceList = null;
//		if(BaseController.hasRole(SysConstants.ADMIN)){
//			resourceList =  resourceDao.findLeftMenuByUser();
//		}else{
//			User user = userDao.findOne(BaseController.getCurrentUserId());
//			List<String> permissions  = getPermissionList(user);
//			if(permissions.isEmpty()) return null;
//			
//			resourceList = resourceDao.findLeftMenuByUser(permissions);
////			resourceList = resourceDao.findLeftMenuBySubadmin();
//		}
//		
//		if(resourceList==null || resourceList.isEmpty()) return null;
//		
//		Map<Long,ResourceVo> map = Maps.newHashMap();
//		for(Resource resource:resourceList){
//			if(resource.getType() == 0){
//				map.put(resource.getId(), BeanMapper.map(resource, ResourceVo.class));
//			}else{
//				ResourceVo headerVo = map.get(Long.valueOf(resource.getType()));
//				if(headerVo==null) continue;
//				Map<String,String> subMap = Maps.newHashMap();
//				subMap.put("name", resource.getName());
//				subMap.put("link", resource.getHyperlink());
//                subMap.put("oldlink", resource.getOldlink());
//				headerVo.getMenuList().add(subMap);
//				map.put(Long.valueOf(resource.getType()), headerVo);
//			}
//		}
//		
//		List<ResourceVo> menuList = Lists.newArrayList();
//        for (Map.Entry<Long,ResourceVo> entry : map.entrySet()) {
//            menuList.add(entry.getValue());
//        }
//		
//		return menuList;
//		
//	}
//	
//	public static List<String> getPermissionList(User user){
//		Set<String> permSet = new HashSet<String>(); 
////		for(Role role:user.getRoleList()){
////			String[] perms = StringUtils.split(role.getPermissions(), ",");
////			for(String perm : perms){
////				permSet.add(StringUtils.substringBefore(perm, ":"));
////			}
////		}
//		return new ArrayList<String>(permSet);
//	}
//	
//	@Transactional(readOnly = false)
//	public void updateLastLoginOn(Long userId,Date lastLoginOn,String clientFlag) {
//		try {
//
//            UserInfo user = userInfoDao.findOne(userId);
//            user.setLastLogin(lastLoginOn);
//            userInfoDao.save(user);
//		} catch (Exception e) {
//			logger.error("updateLastLoginOn error:",e);
//		}
//		
//	}
//
//
//
//}
